package twitter.RequestParser;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import twitter.Database.TwitterMaintainer;
import twitter.TwitterLibrary.Utility;
/****************************************************
 * this class is designed for parse all the requests that back end server receives
 * @author fang
 *
 */
public class BackendRequestParser {
	protected TwitterMaintainer twitterMaintainer;
	private DocumentBuilderFactory factory = null;
	protected Utility utility=null;
	protected String result;
	/********************************************
	 * Class constructor
	 * @param twitterMaintainer different backendrequestparser objects run in 
	 * different threads all using the same twitterMaintainer object
	 */
	public BackendRequestParser(TwitterMaintainer twitterMaintainer,Utility utility)
	{
		this.twitterMaintainer=twitterMaintainer;
		
		factory = DocumentBuilderFactory.newInstance();
		this.utility=utility;
	}
	
	public void parseGet(NamedNodeMap Atts)
	{
		
		Node tagNode=Atts.getNamedItem("tag");
		String tag=tagNode.getNodeValue();
		Node returnTpyeNode =Atts.getNamedItem("returntype");
		String returntype=returnTpyeNode.getNodeValue();
		DocumentBuilder db = null;
		try {
			db = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(returntype.equals("1"))
		{
			
			/*
			 * response: <responses tag="tag"> <response status="status1" version="1" >
			 * </response> <response status="status2" version="2" > </response>
			 * </responses>
			 */
			ArrayList<ArrayList<String>> rs=this.twitterMaintainer.searchStatusByTag(tag);
			
			
			Document DOC_responses = db.newDocument();

			Element DB_responses = DOC_responses.createElement("responses");
			DB_responses.setAttribute("tag", tag);
			DOC_responses.appendChild(DB_responses);
			
			
				//for(int i=0;i<rs.size();i++)
				//{
					ArrayList<String> statusList=rs.get(0);
					ArrayList<String> versionList=rs.get(1);
					for(int i=0;i<statusList.size();i++)
					{
						String status=statusList.get(i).toString();
						String version=versionList.get(i).toString();
						Element response = DOC_responses.createElement("response");
						
						response.setAttribute("status", status);
						response.setAttribute("version", version);
						
						//DOC_responses.appendChild(DB_responses);
					
						DB_responses.appendChild(response);
					}
				
			 result=utility.xmlToString(DOC_responses);
		
			
		}
		else
		{
			
			/*<responses>
			 * <response maxversion="max" tag="tag">
			 * </response>
			 * </responses>
			 * 
			 */
			int version=this.twitterMaintainer.searchForVersion(tag);
			Document DOC_version=db.newDocument();
			Element DB_responses = DOC_version.createElement("responses");
			DOC_version.appendChild(DB_responses);
			Element versionElement = DOC_version.createElement("response");
			versionElement.setAttribute("maxversion", String.valueOf(version));
			versionElement.setAttribute("tag", tag);
			DB_responses.appendChild(versionElement);
			result=utility.xmlToString(DOC_version);
		
			
		}
				
	}
	/********************************************************
	 * readQueries
	 * there are 3 types of queries:
	 *  <query httptype=\"GET\" returntype=\"1\" tag=\""+tag+"\"></query>   return all the status and hash tag information
	 *  <query httptype="GET" returntype="2" tag="TAG"> just return the mav version of the specified hashtag
	 *  <query httptype=\"POST\" status=\"newStatus\" ></query> post new status into twitter database
	 *  
	 */
	public String readQueries(String query)
	{
		Document DOC_queries=null;
		try {
			 DOC_queries=utility.stringToDom(query);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Node queriesNode = DOC_queries.getFirstChild();
		NamedNodeMap Atts = queriesNode.getAttributes();
		Node httpType=Atts.getNamedItem("httptype");
		
		String result="";
		
		if(httpType.getNodeValue().equals("GET"))
		{
			parseGet(Atts);
			
		}/*
		 * <query httptype=\"POST\" status=\"newStatus\" ></query>
		 * 
		 * return
		 * <response>success</response>
		 */
		else if(httpType.getNodeValue().equals("POST"))
		{
			
			parsePost(Atts,query);
			System.out.println("result : "+result);
			return result;
		}
		return result;
	}
	public void parsePost(NamedNodeMap Atts,String query)
	{
		
		Node statusNode=Atts.getNamedItem("status");
		String status=statusNode.getNodeValue();
		DocumentBuilder db = null;
		try {
			db = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.twitterMaintainer.addNewStatus(status);
		
		Document Doc_success=db.newDocument();
		Element DB_response = Doc_success.createElement("response");
		Doc_success.appendChild(DB_response);
		DB_response.setTextContent("success");
		result=utility.xmlToString(Doc_success);
	
	}
	

	
}
